package com.googlecode.androidkurskoblenz.threedcube;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Locale;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.VirtualUniverse;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.vecmath.Vector3f;

import com.googlecode.androidkurskoblenz.jledcube.LedCube;

public class ThreeDLedCube extends LedCube
{
	private CameraView leftCamera;
	private Canvas3D leftCanvas, rightCanvas;
	private Locale locale;
	private CameraView rightCamera;
	private VirtualUniverse universe;
	
	@Override
	protected void addBranchGroup(BranchGroup bg)
	{
		locale.addBranchGraph(bg);
	}
	
	@Override
	protected VirtualUniverse createUniverse()
	{
		JFrame frame = new JFrame("MultipleViews");
		
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		
		universe = new VirtualUniverse();
		locale = new Locale(universe);
		
		float eyeDistance = 2;
		Transform3D trfCamera;
		
		leftCamera = new CameraView();
		TransformGroup vpTG = leftCamera.getViewPlatformTransformGroup();
		trfCamera = new Transform3D();
		trfCamera.setTranslation(new Vector3f(eyeDistance / 2, 0.0f, LedCube.CAMERA_Z));
		vpTG.setTransform(trfCamera);
		
		rightCamera = new CameraView();
		vpTG = rightCamera.getViewPlatformTransformGroup();
		trfCamera = new Transform3D();
		trfCamera.setTranslation(new Vector3f(-eyeDistance / 2, 0.0f, LedCube.CAMERA_Z));
		vpTG.setTransform(trfCamera);
		
		locale.addBranchGraph(leftCamera.getRootBG());
		locale.addBranchGraph(rightCamera.getRootBG());
		
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BorderLayout());
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		
		leftCanvas = leftCamera.getCanvas3D();
		rightCanvas = rightCamera.getCanvas3D();
		leftCanvas.setSize(new Dimension(300, 400));
		rightCanvas.setSize(new Dimension(300, 400));
		
		leftPanel.add(leftCanvas, BorderLayout.CENTER);
		rightPanel.add(rightCanvas, BorderLayout.CENTER);
		
		JPanel mPanel = new JPanel();
		mPanel.setLayout(new GridLayout(0, 2, 0, 0));
		mPanel.add(rightPanel);
		mPanel.add(leftPanel);
		
		frame.getContentPane().add(mPanel);
		
		// frame.setSize( 600, 400 );
		frame.pack();
		frame.setVisible(true);
		return universe;
	}
	
	@Override
	protected Component getCanvas()
	{
		return leftCanvas;
	}
}
